//
//  YCFriendSmartViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/11.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  智能推荐

#import "YCFriendSmartViewController.h"
#import "YCFriendSmartCollectionViewCell.h"
#import "YCFriendHomeViewController.h"

@interface YCFriendSmartViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *friendCollectionView; // 推荐
@property (nonatomic, strong) UICollectionViewFlowLayout *collectionFlowLayout;

/** 标签  */
@property (nonatomic, strong) UILabel *tipsLabel;//为你找到了'?'个朋友
/** 刷新  */
@property (nonatomic, strong) UIButton *refreshButton;

@end

@implementation YCFriendSmartViewController

// 卡片(好友展示)
- (UICollectionView *)friendCollectionView
{
    if (!_friendCollectionView)
    {
        _collectionFlowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionFlowLayout.minimumInteritemSpacing = 2;
        _collectionFlowLayout.minimumLineSpacing = 20;
        _collectionFlowLayout.itemSize = CGSizeMake((kScreenWidth-80)/3.0,(kScreenWidth-80)/3.0+45);
        _collectionFlowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
        
        __block CGFloat height = 0;
        [YCAdapter performOtherBlock:^{
            height = ((kScreenWidth-80)/3.0+45)*2+60;
        } IPhone6Block:^{
            height = ((kScreenWidth-80)/3.0+45)*3 +80;
        } IPhone6pBlock:^{
            height = ((kScreenWidth-80)/3.0+45)*3 +80;
        }];
        
        _friendCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth,height) collectionViewLayout:_collectionFlowLayout];
        _friendCollectionView.dataSource = self;
        _friendCollectionView.pagingEnabled = YES;
        _friendCollectionView.delegate = self;
        _friendCollectionView.backgroundColor = [UIColor clearColor];//
        _friendCollectionView.showsHorizontalScrollIndicator = NO;
        _friendCollectionView.showsVerticalScrollIndicator = NO;
        [_friendCollectionView registerClass:[YCFriendSmartCollectionViewCell class] forCellWithReuseIdentifier:@"friendSmartCollectionViewCell"];
    }
    return _friendCollectionView;
}

- (UILabel *)tipsLabel
{
    if (!_tipsLabel) {
        UILabel *tipsLabel = [[UILabel alloc] init];
        tipsLabel.font = [UIFont systemFontOfSize:13];
        tipsLabel.textColor = [UIColor whiteColor];
        tipsLabel.text = @"为你找到9位朋友";
        tipsLabel.textAlignment = NSTextAlignmentCenter;
        _tipsLabel = tipsLabel;
    }
    return _tipsLabel;
}

- (UIButton *)refreshButton
{
    if (!_refreshButton) {
        UIButton *refreshButton = [[UIButton alloc] init];
        refreshButton.titleLabel.font = [UIFont systemFontOfSize:17];
//        refreshButton.backgroundColor = [UIColor yc_colorWithUInt:0x3ba4fa];
        [refreshButton setTitle:@"刷新" forState:UIControlStateNormal];
        [refreshButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [refreshButton addTarget:self action:@selector(didTouchUpinsideRefreshButton:) forControlEvents:UIControlEventTouchUpInside];
        _refreshButton = refreshButton;
    }
    return _refreshButton;
}

#pragma mark - 生命周期

- (void)loadView
{
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    bgImageView.image = [UIImage imageNamed:@"o_topBgImg"];
    bgImageView.userInteractionEnabled = YES;
    self.view = bgImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"智能推荐";
    [self yc_addBackBarButtonItem];
    
    // 添加背景
//    UIImageView *bgView = [UIImageView alloc] initWithImage:<#(nullable UIImage *)#>
    [self.view addSubview:self.friendCollectionView];
    [self.view addSubview:self.tipsLabel];
    [self.view addSubview:self.refreshButton];
    
    [self.refreshButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.view).offset(-20);
        make.width.equalTo(@60);
    }];
    
    [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.refreshButton.mas_top).offset(-30);
        make.centerX.equalTo(self.view);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UICollectionView datasource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger count = 10;
   
    return count;
}

#pragma mark - UICollectionView delegate

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        
    YCFriendSmartCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"friendSmartCollectionViewCell" forIndexPath:indexPath];
    
    [cell.iconImage yc_setWebImageWithURLString:@"http://b.hiphotos.baidu.com/image/h%3D200/sign=8de92df279cb0a469a228c395b63f63e/8c1001e93901213f391481d456e736d12f2e9597.jpg" holderSize:self.collectionFlowLayout.itemSize];
    cell.nameLabel.text = @"Ailsa";
    cell.addressLabel.text = @"广东 广州";
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YCLog(@"点击了cell中的collectionView的cell的第:%zd个item",indexPath.item);
    
    YCFriendHomeViewController *friendHomeVc = [[YCFriendHomeViewController alloc] init];
    [self presentViewController:friendHomeVc animated:YES completion:nil];
}

#pragma mark - 点击事件
- (void)didTouchUpinsideRefreshButton:(UIButton *)button
{
    YCLogFunc
}


@end
